package com.zq.app.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.zq.app.domain.Student;
public class StudentDaoSpringJdbcImpl implements StudentDao{
	
	private JdbcTemplate jt;
	

	public List<Student> query() {
		// TODO Auto-generated method stub
		final List<Student> stus = new ArrayList<Student>();
		String sql="select id ,name ,age from student";
		jt.query(sql, new RowCallbackHandler(){

			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				while(rs.next()){
					int id = rs.getInt("id");
					String name = rs.getString("name");
					int age = rs.getInt("age");
					Student s = new Student(id,name,age);
					stus.add(s);
				}
			}
			
		});
		return stus;
	}

	public void save(Student stu) {
		// TODO Auto-generated method stub
		String sql="insert into student (name ,age) values(?,?)";
		jt.update(sql, stu.getName(),stu.getAge());
		
	}

	public JdbcTemplate getJt() {
		return jt;
	}

	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}

}
